<template>
  <div id="show_info">
    <div class="show_info">
      <div class="show_info_header">
        <div class="show_info_header_title">
          <template v-if="roomType === 'group'">群聊信息</template>
          <template v-else-if="roomType === 'double'">好友信息</template>
          <Button type="info" @click="chat.set_current_show_info_room_info(null)">关闭</Button>
        </div>
        <div class="show_info_header_icon" @click="showMore">
          <i class="iconfont icon-more"></i>
        </div>
      </div>
      <div class="show_info_body">
        <template v-if="roomType === 'group'">
          <div class="show_info_body_item"><span>房间ID：</span>{{ roomInfo.room_id }}</div>
          <div class="show_info_body_item"><span>创建：</span>{{ roomInfo.created_at }}</div>
          <div class="show_info_body_item"><span>状态：</span>{{ roomInfo.is_active ? '活跃' : '非活跃' }}</div>
          <div class="show_info_body_item">
            <span>成员ID：</span>
            <span v-if="userIds.length">{{ userIds.join(', ') }}</span>
            <span v-else>无</span>
          </div>
        </template>
        <template v-else-if="roomType === 'double' && friendInfo">
          <div class="show_info_body_item"><span>用户名：</span>{{ friendInfo.username }}</div>
          <div class="show_info_body_item"><span>ID：</span>{{ friendInfo.id }}</div>
          <div class="show_info_body_item"><span>邮箱：</span>{{ friendInfo.email }}</div>
          <div class="show_info_body_item"><span>性别：</span>{{ friendInfo.sex }}</div>
          <div class="show_info_body_item"><span>年龄：</span>{{ friendInfo.age }}</div>
          <div class="show_info_body_item"><span>简介：</span>{{ friendInfo.introduction }}</div>
        </template>
        <template v-else>
          <div class="show_info_body_item">无可用信息</div>
        </template>
        <div v-for="(i, idx) in funccollections" :key="idx" class="show_info_body_item">
          <button @click="i.onclick">{{ i.name }}</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { inject, ref, computed, watch, onMounted } from 'vue'
import { get_room_info_all_by_room_id_type } from '@/api/chat/room'

const chat = inject('chat')()
const info = ref(null)

const singleinfo = computed(() => chat.value.get_current_show_info_room_info())

watch(
  () => singleinfo.value,
  async (newV) => {
    info.value = await get_room_info_all_by_room_id_type(
      newV.room_id,
      newV.type,
      chat.value.get_user_info()?.id,
    )
  }
)

const funccollections = ref([])

const roomType = computed(() => info.value?.room_info?.type || '')
const roomInfo = computed(() => info.value?.room_info || {})
const friendInfo = computed(() => (roomType.value === 'double' ? info.value?.friend_info || {} : null))
const userIds = computed(() => {
  if (roomType.value === 'group' && info.value?.room_info?.user_info) {
    return Object.keys(info.value?.room_info?.user_info)
  }
  return []
})

onMounted(async () => {
  info.value = await get_room_info_all_by_room_id_type(
    singleinfo.value.room_id,
    singleinfo.value.type,
    chat.value.get_user_info()?.id,
  )
  console.log(info.value.room_info.user_info)
})
</script>

<style scoped>
#show_info {
  display: flex;
  justify-content: center;
  align-items: center;
}
.show_info {
  background: #fff;
  border-radius: 10px;
  padding: 18px 24px;
  min-width: 320px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.08);
  font-family: "Microsoft YaHei", Arial, sans-serif;
}
.show_info_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 10px;
}
.show_info_header_title {
  font-size: 20px;
  font-weight: 600;
  color: #2c3e50;
  display: flex;
  width: 100%;
  justify-content: space-around;
}
.show_info_header_icon {
  cursor: pointer;
  font-size: 18px;
  color: #8a8a8a;
  transition: color 0.2s;
}
.show_info_header_icon:hover {
  color: #409eff;
}
.show_info_body {
  margin-top: 16px;
}
.show_info_body_item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  font-size: 15px;
  color: #444;
  max-height: 200px;
  overflow: scroll;
}
.show_info_body_item::-webkit-scrollbar{
  display: none
}
.show_info_body_item span {
  color: #888;
  margin-right: 8px;
  min-width: 56px;
  display: inline-block;
}
.show_info_body_item button {
  margin-left: 10px;
  padding: 5px 16px;
  background: #409eff;
  border: none;
  color: #fff;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.2s;
}
.show_info_body_item button:hover {
  background: #337ecc;
}
</style>